import java.io.*;
import java.net.*;
import java.util.*;
import java.security.Security;
import java.security.Provider;
import javax.net.ssl.*;
import local.util.MyPrint;
import java.text.DecimalFormat;

import local.util.MyStringFormat;
import local.util.MyPrint;

public class DownloadYahooData {    

    private static boolean debug = false;
    private static boolean save = false;
    private String myUrl = null;
    private String urlReturnString = new String();
    private Vector urlReturnVector= new Vector(10);

    public static void main (String[] args) {

	DownloadYahooData  mydl = new DownloadYahooData ();
	mydl.downloadData();
    }

    public void downloadData () { 
	String url_return_string = null;
	Vector url_return_vector = new Vector ();
	Vector table_head_v = new Vector ();
	Vector table_record_v_v = new Vector ();
	Calendar c = Calendar.getInstance();

	setUrl("http://table.finance.yahoo.com/table.csv?a=12&b=1&c=2002&d=12&e=31&f=2002&s=^ixic&y=0&g=d&ignore=.csv");
	connectToUrl ();
 
    }

    public void setUrl (String url) {
	myUrl = url;
    }

    public void connectToUrl () {

	String url_return_string = "";
	Vector colum_data_v = new Vector ();

	URL url ;

	HttpsURLConnection httpsConnection = null;

	try {            
	    url = new URL (myUrl);

	    try {              

		StringBuffer buff1= new StringBuffer("");  
		if (myUrl.startsWith("https:")) {
		    httpsConnection = (HttpsURLConnection)url.openConnection();                
		    httpsConnection.setDoOutput(false);                
		    httpsConnection.connect();                

		    if (debug) {
			System.out.println("Connection Code and Message:" + 
					   httpsConnection.getResponseCode() + 
					   " - " + httpsConnection.getResponseMessage()+"\n");
		    }

		    BufferedReader in = new BufferedReader(new InputStreamReader
			(httpsConnection.getInputStream()));

		    url_return_string = in.readLine();

		    while (url_return_string != null) {
			buff1.append(url_return_string);
			url_return_string = in.readLine();
		    }

		    in.close();            
		}
		else if (myUrl.startsWith("http:")) {   

		    HttpURLConnection httpConnection = null;
		    httpConnection = (HttpURLConnection)url.openConnection();                
		    httpConnection.setDoOutput(false);                
		    httpConnection.connect();                

		    if (debug) {
			System.out.println("Connection Code and Message:" + 
					   httpConnection.getResponseCode() + 
					   " - " + httpConnection.getResponseMessage()+"\n");
		    }

		    BufferedReader in = new BufferedReader(new InputStreamReader
			(httpConnection.getInputStream()));

		    url_return_string = in.readLine();
		    
		    while (url_return_string != null) {
			if (url_return_string.startsWith("Date")) {
			    MyPrint.printLine("=",100);
			    System.out.println(url_return_string);
			    MyPrint.printLine("=",100);
			}
			else {
			    String element = "";
			    StringTokenizer tokens = new StringTokenizer(url_return_string);
			    while (tokens.hasMoreTokens()) {
				insertIntoDataBase( "INSERT INTO Nasdaq VALUES ('"+
						    (String)tokens.nextToken(",")+"',"+
						    (String)tokens.nextToken(",")+"," +
						    (String)tokens.nextToken(",")+"," +
						    (String)tokens.nextToken(",")+"," +
						    (String)tokens.nextToken(",")+"," +
						    (String)tokens.nextToken(",")+ ");");
			    }
			}
			url_return_string = in.readLine();
		    }
		    MyPrint.printVector(colum_data_v,6);
		    in.close();            
		}	       
		url_return_string = buff1.toString();
		urlReturnString = url_return_string;
	    } catch (IOException ioe) {                
		System.out.println(ioe.getMessage());  
		System.exit(1);            
	    }        
	} catch (MalformedURLException mue) {            
	     System.out.println(mue.getMessage());            
	     System.exit(1);        
	} catch (Exception e) {            
	    e.printStackTrace();            
	    System.exit(1);        
	}    
    }

    public String getUrlReturnString () {
	return urlReturnString;
    }

    public Vector stringToVector (String input) {

	Vector url_return_vector = new Vector ();
	String element = null;
	StringTokenizer tokens = new StringTokenizer(input, ",");
	while (tokens.hasMoreTokens()) {
	    element = tokens.nextToken();
	    if (element != null) {
		url_return_vector.addElement(element);
	    }
	}

	return url_return_vector;
    }

    private void insertIntoDataBase( String queryStr) {

	DataBaseInsert myInsert = new DataBaseInsert();	
	myInsert.setQueryString(queryStr);
	if(myInsert.insertData()==0) {
	    System.exit(0);
	}
    }         
}








